package com.pshcp.patient.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.common.enums.RegTypeEnum;
import com.pshcp.domain.dto.DeptDTO;
import com.pshcp.domain.dto.DeptDescDTO;
import com.pshcp.domain.model.Dept;
import com.pshcp.domain.model.DeptDesc;
import com.pshcp.domain.model.DeptGroup;
import com.pshcp.domain.model.DeptGroupRel;
import com.pshcp.domain.vo.DeptVO;
import com.pshcp.patient.domain.dept.ao.DeptDescAO;
import com.pshcp.patient.domain.dept.ao.DeptDescDetailAO;
import com.pshcp.patient.domain.dept.ao.DeptDetailAO;
import com.pshcp.patient.domain.dept.ao.DeptListAO;
import com.pshcp.patient.domain.dept.ao.DeptTreeAO;
import com.pshcp.patient.domain.dept.vo.DeptDescVO;
import com.pshcp.patient.domain.dept.vo.DeptTreeVO;
import com.pshcp.patient.tool.DeptTools;
import com.pshcp.service.DeptDescService;
import com.pshcp.service.DeptGroupRelService;
import com.pshcp.service.DeptGroupService;
import com.pshcp.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 科室
 * @Author: xiaoche
 * @Date: 2022/10/29 23:38
 */
@Api(tags = {"患者端-科室"})
@Slf4j
@RestController
@RequestMapping("/api/patient/dept")
public class DeptAPI {

    @Autowired
    private DeptGroupService deptGroupService;

    @Autowired
    private DeptGroupRelService deptGroupRelService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private DeptDescService deptDescService;

    /**
     * 当日挂号 - 核酸医生
     */
    private static String todayRegDeptMatch = "核酸";

    @ApiOperation("科室树")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "科室树")
    @PostMapping("/tree")
    public ResultDTO<List<DeptTreeVO>> tree(@RequestBody DeptTreeAO deptTreeAO) {
        if(ObjectUtils.isEmpty(deptTreeAO.getParentDeptId()) || deptTreeAO.getParentDeptId().equals("0")){
            DeptGroup deptGroup = new DeptGroup();
            deptGroup.setStatus(CommonStatusEnum.INIT.getCode());
            deptGroup.setHosArea(deptTreeAO.getHosArea());
            List<DeptGroup> deptGroups = deptGroupService.select(deptGroup);
            return ResultBuilder.success(DeptTools.buildOutTree(deptTreeAO.getRegType(), deptGroups));
        }
        DeptDTO deptDTO = new DeptDTO();
        if(!ObjectUtils.isEmpty(deptTreeAO.getSearch())){
            deptDTO.setHisDeptName(deptTreeAO.getSearch());
        }
        deptDTO.setStatus(CommonStatusEnum.INIT.getCode());
        if(RegTypeEnum.TODAY.getCode().equals(deptTreeAO.getRegType())){
            deptDTO.setHisDeptName(todayRegDeptMatch);
        }else{
            //get dept id by group id
            List<DeptGroupRel> deptGroupRels = deptGroupRelService.findByGroupId(Long.valueOf(deptTreeAO.getParentDeptId()));
            deptDTO.setIds(deptGroupRels.stream().map(d->d.getDeptId()).collect(Collectors.toList()));
        }
        List<Dept> depts = deptService.select(deptDTO);
        return ResultBuilder.success(DeptTools.buildList2Tree(depts));
    }

    @ApiOperation("科室列表")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "科室列表")
    @PostMapping("/list")
    public ResultDTO<List<DeptTreeVO>> list(@RequestBody DeptListAO deptListAO) {
        DeptDTO deptDTO = new DeptDTO();
        if(!ObjectUtils.isEmpty(deptListAO.getSearch())){
            deptDTO.setHisDeptName(deptListAO.getSearch());
            deptDTO.setHisSectName(deptListAO.getSearch());
        }
        deptDTO.setStatus(CommonStatusEnum.INIT.getCode());
        List<Dept> depts = deptService.select(deptDTO);
        return ResultBuilder.success(DeptTools.buildModel2List(depts));
    }

    @ApiOperation("科室详情")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "科室详情")
    @PostMapping("/detail")
    public ResultDTO<DeptVO> detail(@RequestBody DeptDetailAO deptDetailAO) throws BizException {
        CommonTool.checkObj(deptDetailAO.getDeptId(), "10030001");
        DeptDTO deptDTO = new DeptDTO();
        deptDTO.setHisDeptId(deptDetailAO.getDeptId());
        deptDTO.setStatus(CommonStatusEnum.INIT.getCode());
        List<Dept> depts = deptService.select(deptDTO);
        if(ObjectUtils.isEmpty(depts)){
            return ResultBuilder.success();
        }
        return ResultBuilder.success(CommonTool.copyProperties(depts.get(0), DeptVO.class));
    }

    @ApiOperation("科室介绍列表")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "科室介绍列表")
    @PostMapping("/descList")
    public ResultDTO<List<DeptDescVO>> descList(@RequestBody DeptDescAO deptDescAO) {
        DeptDescDTO deptDTO = new DeptDescDTO();
        deptDTO.setPageSize(999);
        if(!ObjectUtils.isEmpty(deptDescAO.getSearch())){
            deptDTO.setDeptName(deptDescAO.getSearch());
        }
        deptDTO.setStatus(CommonStatusEnum.INIT.getCode());
        deptDTO.setHosArea(deptDescAO.getHosArea());
        PageList<DeptDesc> depts = deptDescService.list(deptDTO);
        return ResultBuilder.success(CommonTool.copyPropertiesList(depts.getItems(), DeptDescVO.class));
    }

    @ApiOperation("科室介绍详情")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "科室介绍详情")
    @PostMapping("/descDetail")
    public ResultDTO<DeptDescVO> descDetail(@RequestBody DeptDescDetailAO detailAO) throws BizException {
        CommonTool.checkObj(detailAO.getId(), "10030001");
        DeptDesc deptDesc = deptDescService.findById(detailAO.getId());
        if(ObjectUtils.isEmpty(deptDesc)){
            return ResultBuilder.success();
        }
        return ResultBuilder.success(CommonTool.copyProperties(deptDesc, DeptDescVO.class));
    }

}
